package core

import (
	"fmt"
	"gitee.com/gitee-go/core/common"
	"github.com/gin-gonic/gin"
	"net/http"
)

type UIRes struct {
	Code   int         `json:"code"`
	Msg    string      `json:"msg"`
	Data   interface{} `json:"data"`
	msgs   []interface{}
	lc     string
	status int
}

func NewUIRes(code int) *UIRes {
	return &UIRes{Code: code}
}
func NewUIResOk(data ...interface{}) *UIRes {
	c := &UIRes{Code: common.UICodeOk}
	if len(data) > 0 {
		c.Data = data[0]
	}
	return c
}

func (c *UIRes) SetCode(code int) *UIRes {
	c.Code = code
	return c
}
func (c *UIRes) SetStatus(a int) *UIRes {
	c.status = a
	return c
}
func (c *UIRes) SetMsgf(format string, args ...interface{}) *UIRes {
	c.Msg = fmt.Sprintf(format, args...)
	return c
}
func (c *UIRes) SetLc(lc string) *UIRes {
	c.lc = lc
	return c
}
func (c *UIRes) SetMsgObjs(args ...interface{}) *UIRes {
	c.msgs = append(c.msgs, args...)
	return c
}
func (c *UIRes) SetData(data interface{}) *UIRes {
	c.Data = data
	return c
}
func (c *UIRes) ResJson(g *gin.Context, format ...bool) {
	if c.Msg == "" {
		if c.lc == "" {
			c.lc = "zh" //默认
		}
		c.Msg = common.GetUIMsg(c.lc, c.Code, c.msgs...)
	}
	stat := http.StatusOK
	if c.status != 0 {
		stat = c.status
	}
	if len(format) > 0 && format[0] {
		g.IndentedJSON(stat, c)
	} else {
		g.JSON(stat, c)
	}
}
